<template>
    <view class="content">
        <van-popup position="bottom" @click-overlay="closePopup" :show="show" round z-index=104
            custom-style="width: 100%;background:#ffffff;overflow: hidden; ">
            <view class="popup-content">
                <scroll-view :refresher-enabled="false" class="scroll-content" scroll-y :lower-threshold="80"  show-scrollbar="false">

                <view class="popup-main">
                <view style="display: flex; justify-content: space-between;">
                    <view class="popupTitle"> {{title}}</view>
                    <van-icon name="cross"  size="15" @click="closePopup" />
                </view>
                    <!-- 服务信息 -->
                <view class="serviceInfo">
                    <view class="serviceInfoContent">
                        <!-- <view class="serviceInfoLabel">服务编号： <span>45861441122111</span></view> -->
                        <view class="serviceInfoLabel">预约地址： <span>{{reservationInfo.addressInfo.detail}}</span></view>
                        <view class="serviceInfoLabel">联系电话： <span>{{reservationInfo.userInfo.phone}}</span></view>
                        <view class="serviceInfoLabel">服务时间： <span>{{selectedDateInfo}}</span></view>
                    </view>
                </view>
                <!-- 服务人员 Service Staff-->
                <view class="serviceStaffInfo">
                    <view class="serviceStaffInfoTitle">服务人员</view>
                    <view class="serviceStaffInfoContent">
                        <view class="serviceStaffImg"><img :src="selectedStaffInfo.avatarUrl?selectedStaffInfo.avatarUrl: defaultAvatar "  alt=""></view>
                        <view class="serviceStaffText">
                            <view class="staffName">{{selectedStaffInfo.name}} <span>{{selectedStaffInfo.workYear}}年工作经验</span></view>
                            <view class="staffInfoLabel">
                                <view class="labelItem"  v-for="(item,index) in selectedStaffInfo.labels" :key="index" :class="index===0 ? 'labelItem' : (index===1 ? 'blueItem' : (index==2 ? 'greenItem':'') )">
                                    {{item.name}}
                                </view>
                            </view>
                        </view>
                        <!-- <view class="serviceInfoTel" > <van-icon name="phone" siza="22"/> </view> -->
                    </view>
                </view>
                </view>
                </scroll-view>

                <view class="goodsAction">
                    <!-- <van-goods-action>
                        <van-goods-action-button color="#FF6203" text="确认预约" @click="submitPreBooked" />
                    </van-goods-action> -->
                    <van-button type="primary" @click="submitPreBooked" :disabled="isSubmitting"
                    :class="{ 'disabled-btn': isSubmitting }">确认预约</van-button>

                    <view class="cancelTips">
                        如行程有变，需至少提前N天/时间进行取消，否则将会扣除对应违约责次数，每个用户限免N次 <span @tap="toAgreement">《取消预约协议》</span>
                    </view>
                </view>
            </view>
        </van-popup>
    </view>
</template>

<script>
import Toast from '@/wxcomponents/vant/toast/toast'
import {orderServiceApi,preOrderServiceApi} from '@/api/service'
import { SOURCE_TYPE } from '@/utils/constant'

export default {
    props: {
        show: {
            type: Boolean,
            default: () => false
        },
        hasStaff:{
            type: Boolean,
            default: () => false
        },
        reservationInfo:{
            type: Object,
            default: () => {}
        }

    },
    data () {
        return {
            defaultAvatar:'https://17lelife-bucket.oss-cn-beijing.aliyuncs.com/WeChat-App-Picture/defaultAvatar.png',
            curS:0,
            curSName:'',
            date: '',
            showMark: true, // 标记
            title: '预约信息',
            option: {},
            labelList:['金牌保姆','会开车','育儿经验'],
            selectedDateInfo:null,
            selectedStaffInfo:{}, // 选中的服务人员信息
            isSubmitting: false,  // 防抖状态
            submitTimer: null    // 防抖计时器
        }
    },

    computed: {
    },


  watch:{
    show: {
        immediate: true,
        handler(newVal) {
            console.log(newVal, 'newVal',this.hasStaff,'hasStaff')
            if (newVal && this.reservationInfo?.orderDateInfo) {
                this.formatSelectedDate()
            }

            if(newVal && !this.hasStaff){
                console.log(this.hasStaff)
               this.preOrderService()
            }
        }
    },
    reservationInfo: {
        deep: true,
        handler(newVal) {
            if (this.show && newVal?.orderDateInfo) {
                this.formatSelectedDate()
            }
            if(this.show){
                console.log(this.reservationInfo)
                this.selectedStaffInfo = this.reservationInfo?.selectedStaffInfo || {}
            }
        }
    }
  },
    onLoad () {
        if (this.house===0) {
            // this.title = '商品排序'
        } else{
            // this.title = '全部分类'
        }
      
    },
    onReady () { },

    methods: {
  
        // 关闭弹窗
        closePopup () {
            console.log('关闭 关闭关闭---------')
            this.$emit('closePreBookedInfoPopup')
        },
        // 确定
        submitPreBooked(){
            if (this.isSubmitting) return;
            this.isSubmitting = true;

            const apiParams={
                reservationId:this.reservationInfo.reservationId,
                orderDate:this.reservationInfo.orderDateInfo.date,
                orderTime:this.reservationInfo.orderTime,
                lat:this.reservationInfo.userInfo.latitude,
                lng:this.reservationInfo.userInfo.longitude,
                empId:this.hasStaff ? this.reservationInfo.selectedStaffInfo.id :this.selectedStaffInfo.id,
                contactPhone:this.reservationInfo.userInfo.phone,
                addressDetail:this.reservationInfo.addressInfo.detail,
                houseNumber:this.reservationInfo.addressInfo.houseNumber,
                contactName:this.reservationInfo.userInfo.realName,
                province:this.reservationInfo.addressInfo.provinceName,
                city:this.reservationInfo.addressInfo.cityName,
                district:this.reservationInfo.addressInfo.districtName,
                versionNo:this.reservationInfo.versionNo,
                selectType:this.hasStaff ? 'self':'system' ,//system,self
            }
            console.log(apiParams,'apiParams')
            clearTimeout(this.submitTimer);
            this.submitTimer = setTimeout(() => {

            orderServiceApi(apiParams).then(res=>{
                if(res.code=200){
                    console.log(res,'预约成功')
                    Toast.success('预约成功')
                    this.$emit('closePreBookedInfoPopup',false)

                    const params = {
                        reservationId:this.reservationInfo.reservationId,
                        source: SOURCE_TYPE.SERVICE,
                        bookedDateInfo:this.selectedDateInfo,
                        userInfo:this.reservationInfo.userInfo,
                        bookedAddress:this.reservationInfo.addressInfo.detail,
                    }
                    uni.navigateTo({
                        url:
                            '/pages/myService/successPage?params=' +
                            encodeURIComponent(JSON.stringify(params))
                    })
                }
               
            }).catch(err => {
                    // Toast.fail('预约失败');
                }).finally(() => {
                    this.isSubmitting = false;
                });
            }, 500);
        },
        toAgreement(){
            uni.navigateTo({
                url:
                    '/pages/other/cancellationAgreement'
            })
        },

        // 对预约时间进行处理，拼接时间信息
        formatSelectedDate() {
            const dateStr = this.reservationInfo.orderDateInfo
            const timeStr = this.reservationInfo.orderTime
            console.log(dateStr, timeStr, 'dateStr, timeStr')
            // 拼接时间信息
            this.selectedDateInfo = `${dateStr.date} ${dateStr.week} ${timeStr}`
            console.log(this.selectedDateInfo, 'this.selectedDateInfo')
        },
        // 没有选择服务人员的预约
        preOrderService(){
            const apiParams={
                reservationId:this.reservationInfo.reservationId,
                orderDate:this.reservationInfo.orderDateInfo.date,
                orderTime:this.reservationInfo.orderTime,
                lat:this.reservationInfo.userInfo.latitude,
                lng:this.reservationInfo.userInfo.longitude,
                // empId:this.hasStaff ? this.reservationInfo.selectedStaffInfo.id :this.selectedStaffInfo.id,
                contactPhone:this.reservationInfo.userInfo.phone,
                addressDetail:this.reservationInfo.addressInfo.detail,
                houseNumber:this.reservationInfo.addressInfo.houseNumber,
                contactName:this.reservationInfo.userInfo.realName,
                province:this.reservationInfo.addressInfo.provinceName,
                city:this.reservationInfo.addressInfo.cityName,
                district:this.reservationInfo.addressInfo.districtName,
                versionNo:this.reservationInfo.versionNo,
            }
            console.log(apiParams,'apiParams')
            preOrderServiceApi(apiParams).then(res=>{
                if(res.code=200){
                    console.log(res,'')
                    this.selectedStaffInfo = res.data || {}
                    // Toast.success('预约成功')
                    // this.$emit('closePreBookedInfoPopup',false)
                }
            })
        }

  },

}
</script>
<style lang="scss" scoped>

::v-deep .van-button__text {
    display: flex !important;
    flex-direction: column !important;
}
// 提示框
.popup-content {
    padding:24px;
    font-size: 12px;
    background: #ffffff;
    border-radius: 16px;
    overflow: hidden;
    // height: 50%;


    .popup-main {
        // height: 100%
        // height:calc(100% - (144rpx + env(safe-area-inset-bottom)));
        padding-bottom: calc(0rpx + env(safe-area-inset-bottom));
    }


    .popupTitle {
        font-size: 20px;
        text-align: left;
        color: #111111;
        line-height: 20px;
    }

}

// 服务信息
.serviceInfo{
    margin-top: 32rpx;
    // padding:0rpx 48rpx;
    border-bottom: 2rpx solid #f0f0f0;
    .serviceInfoTitle{
        font-size: 36rpx;
        text-align: left;
        color: #333333;
        line-height: 36rpx;
    }
    .serviceInfoContent{
        // margin-top:32rpx;
        // padding-bottom: 40rpx;
        .serviceInfoLabel{
            font-size: 32rpx;
            line-height: 32rpx;
            text-align: left;
            color: #999999;
            margin-bottom: 38rpx;

            span{
                font-size: 32rpx;
                text-align: left;
                color: #333333;
                line-height: 32rpx;
            }
        }
    }
}
// 服务人员
.serviceStaffInfo{
    padding-top: 40rpx;

    .serviceStaffInfoTitle{
        font-size: 36rpx;
        text-align: left;
        color: #333333;
        line-height: 36rpx;
    }
    .serviceStaffInfoContent{
        padding-top: 32rpx;
        padding-bottom: 40rpx;
        display: flex;
        align-items: center;

        .serviceStaffImg{
            // width: 15%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            border-radius: 50%;

            img{
                // width: 100%;
                // height: auto;
                width: 112rpx;
                height: 112rpx;
                border-radius: 50%;
                // margin-right: 24rpx;
            }
        }
        .serviceStaffText{
            margin-left: 24rpx;
            .staffName{
                font-weight: bolder;
                font-size: 32rpx;
                text-align: left;
                color: #333333;
                line-height: 32rpx; 
                display: flex;
                align-items: center;

                span{
                    font-weight: normal;
                    font-size: 24rpx;
                    text-align: left;
                    color: #999999;
                    line-height: 24rpx;
                    padding-left:16rpx;
                }
            }
            .staffInfoLabel{
                display: flex;
                margin-top: 24rpx;

                .labelItem{
                    border-radius: 4rpx;
                    background: #fff4ed;
                    border: 1rpx solid #ff6202;

                    color: #FF6203;
                    font-size: 20rpx;
                    line-height: 20rpx;
                    text-align: center;
                    padding: 10rpx 22rpx;
                    // border-radius: 25rpx;
                    margin-right: 16rpx;
                }
                // .blueItem{
                //     border: 1px solid #0094D9;
                //     background-color: rgb(0, 148, 217, 0.06);
                //     color: #0094D9;
                // }
                // .greenItem{
                //     border: 1px solid #00CB00;
                //     background-color: rgb(0, 203, 0, 0.06);
                //     color: #00CB00;
                // }
                .labelItem:last-child{
                    margin-right: 0rpx;
                }
            }
        }
        .serviceInfoTel{
            text-align: right;
            margin-left: 60rpx;
        }

    }
}

// 去绑定的按钮
.button{
    // width: 100%;
    font-size: 12px;
    color: #ffffff;
    height: 20px;
    line-height:20px;
    border-radius: 10px;
    border-style: initial !important;
    padding: 0 11px !important;
    background: #FF6203 !important;
    margin-left: 13px;
}
.cancelTips{
    margin-top: 32rpx;
    color: #999999;
    font-size: 20rpx;
    
    span{
        color: #FF6203;
    }
}

.goodsAction {
    position: relative;
    
    .cancelTips {
        margin-top: 32rpx;
        color: #999999;
        font-size: 20rpx;
        padding: 0 24rpx;  // 添加左右边距
        // line-height: 1.5;  // 增加行高
        
        span {
            color: #FF6203;
            display: inline-block; // 使协议文字可点击
            margin-top: 16rpx;
        }
    }
    
    // 调整按钮容器高度
    ::v-deep .van-goods-action {
        height: auto !important;
        padding-bottom: 40rpx;
    }
}
// 底部按钮 不可点击状态
.disabled-btn {
    // background-color: #cccccc !important;
    border-color: #cccccc !important;
    color: white !important;
}
::v-deep .van-button{
    width: 100% !important;
}
</style>

